package com.maodou.core.network

import okhttp3.Interceptor
import okhttp3.Response

class HeaderInterceptor(private val headerProvider: () -> Map<String, String>) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val original = chain.request()
        val builder = original.newBuilder()
        try {
            val headers = headerProvider.invoke()
            headers.forEach { (key, value) ->
                if (key.isNotBlank() && value.isNotBlank()) builder.header(key, value)
            }
        } catch (_: Throwable) {
            // ignore header provider errors to avoid blocking requests
        }
        return chain.proceed(builder.build())
    }
}